import sun.security.provider.MD5;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpCookie;

@WebServlet("/user-login")
public class Login extends HttpServlet {
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("doPost方法");
//        resp.setContentType("text/html;charset=utf-8");
//        req.setCharacterEncoding("utf-8");
//        String username=req.getParameter("username");
//        String password=req.getParameter("password");
//        PrintWriter out=resp.getWriter();
//        out.print("<h1>傻逼</h1>");
//        //HandleCookie(req,resp,username,password);
//    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String userValue=null;
        String pwdValue=null;
        PrintWriter out=resp.getWriter();
        boolean flag=false;
        Cookie[] cookies=req.getCookies();
        if (cookies == null && cookies.length==0) {
            if (username == null && password ==null && username=="" && password=="") {
                //1、给用户提出错误指示
                out.print("<h1>用户名及密码不能为空！</h1>");
                req.getRequestDispatcher("/error/error.jsp").forward(req,resp);
            }
            else {
                if (username.equals("admin") && password.equals("123123")) {
                    //req.getRequestDispatcher("index").forward(req,resp);
                    Cookie c=new Cookie("username",username);
                    Cookie c1=new Cookie("password",password);
                    c.setMaxAge(60);
                    c1.setMaxAge(60);
                    resp.addCookie(c);
                    resp.addCookie(c1);
                    System.out.println("第一次登录");
                    resp.sendRedirect("index");
                }
                else {
                    //2、给用户名和密码错误
                    req.getRequestDispatcher("/error/error.jsp").forward(req,resp);

                }
            }
        }
        else {
            for (Cookie c:cookies) {
                if (c.getName().equals("username")){
                     userValue=c.getValue();
                }
                if (c.getName().equals("password")){
                     pwdValue=c.getValue();
                }
                System.out.println("已存在："+userValue+"------"+pwdValue);
                flag=true;
            }
        }
    }

    private static void HandleLogin(HttpServletRequest request, HttpServletResponse response, String username, String password) throws IOException, ServletException {
        if (username == null && password ==null && username=="" && password=="") {
            //1、给用户提出错误指示
            PrintWriter out=response.getWriter();
            out.print("<h1>用户名及密码不能为空！</h1>");
            request.getRequestDispatcher("/error/error.jsp").forward(request,response);
        }
        else {
            if (username.equals("admin") && password.equals("123123")) {
                request.getRequestDispatcher("/Index").forward(request,response);
            }
            else {
                //2、给用户名和密码错误
                request.getRequestDispatcher("/error/error.jsp").forward(request,response);

            }
        }
    }
    private static void HandleCookie(HttpServletRequest request,HttpServletResponse response,String username,String password) throws IOException, ServletException {
        Cookie[] cookies=request.getCookies();
        if (cookies == null && cookies.length!=0) {
            for (Cookie cookie:cookies) {
                if (cookie.getName().equals("username") && cookie.getName().equals("password")) {
                    String userValue = cookie.getValue();
                    String pwdValue=cookie.getValue();
                    request.setAttribute("username",userValue);
                    request.setAttribute("password",pwdValue);
                    System.out.println("获取cookie值"+userValue+":"+pwdValue);
                }
                else {
                    HandleLogin(request,response,username,password);
                    //1、创建一个Cookie
                    Cookie c=new Cookie("username",username);
                    Cookie c1=new Cookie("username",password);
                    //2、设置Cookie存活时间
                    c.setMaxAge(60);
                    c1.setMaxAge(60);
                    //3、将Cookie存放在响应体里面
                    response.addCookie(c);
                    response.addCookie(c1);
                }
            }
        }
    }
}
